<?php
namespace Admin\Controller;
use Think\Controller;
class NewsController extends CommonController{

	//添加资讯新闻
	public function add(){

		if(IS_POST){

			$title = I('post.title');
			if(!empty($title)){

				$data['title'] = $title;
			}else{

				$this->error('标题不能为空!','',1);
			}

			$description = I('post.description');
			if(!empty($description)){

				$data['description'] = $description;
			}else{

				$this->error('简介不能为空!','',1);
			}

			$author = I('post.author');
			if(!empty($author)){

				$data['author'] = $author;
			}else{

				$data['author'] = '佚名';
			}

			$content = I('post.content');
			if(!empty($content)){

				$data['content'] = $content;
			}else{

				$this->error('内容不能为空!','',1);
			}

			$rank = I('post.rank');
			if(!empty($rank)){

				$data['rank'] = $rank;
			}

			if(!empty($_FILES['thumb']['name'])){

				$data['thumb'] =__ROOT__.'/Uploads'.'/'.$this->upload($_FILES['thumb']);
			}else{

				$this->error('略缩图不能为空!','',1);
			}

			$stime = I('post.stime');
			// echo $stime; die;
			if(!empty($stime)){

				$data['stime'] = strtotime($stime);
			}else{

				$data['stime'] = I('server.REQUEST_TIME');
			}

			$data['ctime'] = I('server.REQUEST_TIME');


			$User = M('news');

			$result = $User->data($data)->add();

			//添加标签和文章关联
			$DB = M('news_tags_relationship');
			$tags = I('post.tags');
			foreach ($tags as $key => $value) {
				# code...
				$tags_data[$key]['aid'] = $result;
				$tags_data[$key]['tagid'] = $value;
				$tags_data[$key]['ctime'] = I('server.REQUEST_TIME');
			}
			// print_r($tags_data); die;
			$re = $DB->addAll($tags_data);

			if(!empty($re)){

				$this->success('添加成功',U('Admin/News/lists'));
			}else{

				$this->error('添加失败!');
			}

		}else{

			$DB = M('news_tags');
			$tags = $DB->select();

			$this->assign('tags',$tags);
			$this->display();
		}
	}

		//资讯列表
	public function lists(){

		if(!IS_POST){

			$User = M('news');

			/**
			数据分页显示开始
			**/
			$count      = $User->where(array('active' => 1,'news_type' => 0))->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $User->where(array('active' => 1,'news_type' => 0))->order('rank desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			/**
			数据分页结束
			**/
			$collections = M('collections');
			$comments = M('comments');
			foreach ($list as $k => $v) {
				# code...
				$list[$k]['comment_count'] = $comments->where(array('ctype' => 2,'for_id' => $v['id']))->count();
				$list[$k]['collection_count'] = $collections->where(array('ctype' => 2,'for_id' => $v['id']))->count();
			}

			$this->assign('show',$show);
			$this->assign('list',$list);
			$this->display();
		}
	}


	//删除资讯  删除资讯将数据表active修改为0  1为显示状态 0 为删除状态
	public function del(){

		$id = I('post.id');

		if(!empty($id)){

			$map['id'] = $id;
		}

		$data['active'] = 0;
		$User = M('news');

		$result = $User->where($map)->data($data)->save();

		if(!empty($result)){

			echo json_encode(1);
		}
	}

	//资讯详情显示
	public function details(){

		if(IS_GET){

			$id = I('get.id');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$User = M('news');

			$list = $User->where($map)->find();

			$list['content'] = html_entity_decode($list['content']);

			$this->assign('list',$list);
			$this->display();
		}
	}

	//修改资讯
	public function modify(){

		if(IS_GET){

			$id = I('get.id');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$User = M('news');

			$list = $User->where($map)->find();
			$list['content'] = html_entity_decode($list['content']);

			//获取标签
			$DB_tags = M('news_tags');
			$tags_id = $DB_tags->select();

			$DB_rel = M('news_tags_relationship');
			$tagid = $DB_rel->where(array('aid' => $id))->getField('tagid',true);

			foreach ($tags_id as $key => $value) {
				# code...
				if(in_array($value['id'], $tagid)){

					$tags_id[$key]['checkbox'] = 1;
				}else{
					$tags_id[$key]['checkbox'] = 0;
				}
			}

			// print_r($tag_id); die;
			$this->assign('tags_id',$tags_id);
			$this->assign('list',$list);
			$this->display();
		}else{

			$id = I('post.id');
			if(!empty($id)){

				$map['id'] = $id;
			}else{

				$this->error('参数错误!','',1);
			}

			$title = I('post.title');
			if(!empty($title)){

				$data['title'] = $title;
			}

			$author = I('post.author');
			if(!empty($author)){

				$data['author'] = $author;
			}

			$description = I('post.description');
			if(!empty($description)){

				$data['description'] = $description;
			}

			$content = I('post.content');
			if(!empty($content)){

				$data['content'] = $content;
			}

			$rank = I('post.rank');
			if(!empty($rank)){

				$data['rank'] = $rank;
			}

			if(!empty($_FILES['thumb']['name'])){

				$data['thumb'] =__ROOT__.'/Uploads'.'/'.$this->upload($_FILES['thumb']);
			}

			$stime = I('post.stime');
			if(!empty($stime)){

				$data['stime'] = strtotime($stime);
			}
			// echo $data['stime']; die;
			$data['ctime'] = I('server.REQUEST_TIME');

			$User = M('news');

			$result = $User->where($map)->data($data)->save();

			//修改标签关联
			$tags = I('post.tags');
			

			if(!empty($result)){

				//删除原有的标签关联
				M('news_tags_relationship')->where(array('aid' => $id))->delete();

				foreach ($tags as $key => $value) {
					# code...
					$rel_data[$key]['ctime'] = I('server.REQUEST_TIME');
					$rel_data[$key]['aid'] = $id;
					$rel_data[$key]['tagid'] = $value;
				}

				$modify_re = M('news_tags_relationship')->addAll($rel_data);

				if($modify_re){

					$this->success('修改成功!',U('Admin/News/lists'));
				}else{

					$this->error('修改失败!','',1);
				}
			}else{

				$this->error('修改失败!','',1);
			}

		}

	}


	//图片单个上传
	private function upload($file){    
		$upload = new \Think\Upload();// 实例化上传类    
		$upload->maxSize   =     3145728 ;// 设置附件上传大小    
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
		$upload->savePath  =      'news/'; // 设置附件上传目录    // 上传单个文件     
		$info   =   $upload->uploadOne($file);    
		if(!$info) {
		// 上传错误提示错误信息        
			$this->error($upload->getError());    
		}else{
		// 上传成功 获取上传文件信息         
			return $info['savepath'].$info['savename'];    
		}
	}

	//生成随机图像名称
	public function img_name()
	{
		$time=I('server.REQUEST_TIME');
		return $time.str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
	}


	/**
	 * 资讯用户评论表
	 */
	public function comment_lists(){

		if(IS_GET){
                    
			$type = I('get.ctype');
			$id = I('get.id');
			if(!empty($id)){

				$for_id = $id;
				$ctype = $type;
				$pid = 0; //获取一级评论
			}

			/**
			 * 获取到该项目下所有的评论
			 * 使用公共函数get_comments 参数 for_id关联id   ctype 评论类型
			 */
			$lists = get_comments($ctype,$for_id,$pid);
			// print_r($lists); die;
			$this->assign('lists',$lists);
			$this->assign('for_id',$for_id);
			$this->assign('ctype',$ctype);
			$this->display();
		}
	}


	/**
	 * 资讯用户评论表 二级评论获取
	 */
	public function second_comment_lists(){

		if(IS_GET){

			$id = I('get.id');
			$for_id = I('get.for_id');
			$type = I('get.ctype');
			if(!empty($id)){

				$pid = $id;
				$ctype = $type;
			}
			// echo $pid; echo $for_id;  die;
			/**
			 * 获取到该项目下所有的评论
			 * 使用公共函数get_comments 参数 for_id关联id   ctype 评论类型
			 */
			$lists = get_comments($ctype,$for_id,$pid);
			// print_r($lists); die;
			$this->assign('lists',$lists);
			$this->display();
		}
	}



	/**
	 * 隐藏和显示评论功能
	 */
	public function comments_ishidden(){

		if(IS_AJAX){

			$id = I('post.id');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$hidden = I('post.ishidden');
			if(isset($hidden)){

				$ishidden = $hidden;
			}

			$comments = M('comments');

			if($ishidden == 1){

				$data['ishidden'] = 0;
			}else if($ishidden == 0){

				$data['ishidden'] = 1;
			}

			$result = $comments->where($map)->data($data)->save();

			if($result){

				echo json_encode(1);
			}else{

				echo json_encode(0);
			}
		}
	}



	/**
	 * 资讯关注表
	 */
	public function collection_lists(){

		if(IS_GET){

			$type = I('get.ctype');
			$id = I('get.id');
			// echo $id; die;
			if(!empty($id)){

				$for_id = $id;
				$ctype = $type;
			}
			// echo $for_id; die;
			/**
			 * 获取到该项目下所有的评论
			 * 使用公共函数get_collection 参数 for_id关联id   ctype 评论类型
			 */
			$lists = get_collection($for_id,$ctype);

			$this->assign('lists',$lists);
			$this->display();
		}
	}

	/**
	 * 删除关注
	 */
	public function del_collections(){

		if(IS_AJAX){

			$id = I('post.id');
			// echo $id; die;
			if(!empty($id)){

				$map['id'] = $id;
				// $map['ctype'] = 2;
			}

			$collections = M('collections');

			$result = $collections->where($map)->delete();

			if($result){

				echo json_encode(1);
			}else{

				echo json_encode(0);
			}
		}
	}


	/**
		危害简介开始
	**/
	/**
	 * 危害简介列表
	 * 
	 */
	
	public function harm_lists(){

		if(!IS_POST){

			$User = M('news');

			/**
			**数据分页显示开始
			**/
			$count      = $User->where(array('active' => 1,'news_type' => 1))->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $User->where(array('active' => 1,'news_type' => 1))->order('rank desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			/**
			**数据分页结束
			**/
			$collections = M('collections');
			$comments = M('comments');
			foreach ($list as $k => $v) {
				# code...
				$list[$k]['comment_count'] = $comments->where(array('ctype' => 4,'for_id' => $v['id']))->count();
				$list[$k]['collection_count'] = $collections->where(array('ctype' => 4,'for_id' => $v['id']))->count();
			}

			$this->assign('show',$show);
			$this->assign('list',$list);
			$this->display();
		}
	}
	

	//添加危害简介
	public function add_harm(){

		if(IS_POST){

			$title = I('post.title');
			if(!empty($title)){

				$data['title'] = $title;
			}

			$content = I('post.content');
			if(!empty($content)){

				$data['content'] = $content;
			}

			$rank = I('post.rank');
			if(!empty($rank)){

				$data['rank'] = $rank;
			}

			if(!empty($_FILES['thumb']['name'])){

				$data['thumb'] =__ROOT__.'/Uploads'.'/'.$this->upload($_FILES['thumb']);
			}
			$pid = I('post.pid');
			if(!empty($pid)){

				$data['pid'] = $pid;
			}

			// print_r($data); die;
			$data['ctime'] = I('server.REQUEST_TIME');
			$data['news_type'] = 1;

			$User = M('news');

			$result = $User->data($data)->add();
			if(!empty($result)){

				$this->success('添加成功',U('Admin/News/harm_lists'));
			}else{

				$this->error('添加失败!');
			}

		}else{

			$this->display();
		}
	}


	//资讯详情显示
	public function harm_details(){

		if(IS_GET){

			$id = I('get.id');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$User = M('news');

			$list = $User->where($map)->find();

			$list['content'] = html_entity_decode($list['content']);

			$this->assign('list',$list);
			$this->display();
		}
	}

	//修改资讯
	public function modify_harm(){

		if(IS_GET){

			$id = I('get.id');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$User = M('news');

			$list = $User->where($map)->find();
			$list['content'] = html_entity_decode($list['content']);

			$this->assign('list',$list);
			$this->display();
		}else{

			$id = I('post.id');
			if(!empty($id)){

				$map['id'] = $id;
			}else{

				$this->error('参数错误!','',1);
			}

			$title = I('post.title');
			if(!empty($title)){

				$data['title'] = $title;
			}


			$content = I('post.content');
			if(!empty($content)){

				$data['content'] = $content;
			}

			$rank = I('post.rank');
			if(!empty($rank)){

				$data['rank'] = $rank;
			}

			if(!empty($_FILES['thumb']['name'])){

				$data['thumb'] =__ROOT__.'/Uploads'.'/'.$this->upload($_FILES['thumb']);
			}
			$pid = I('post.pid');
			if(!empty($pid)){

				$data['pid'] = $pid;
			}

			// echo $data['stime']; die;
			$data['ctime'] = I('server.REQUEST_TIME');

			$User = M('news');

			$result = $User->where($map)->data($data)->save();

			if(!empty($result)){

				$this->success('修改成功!',U('Admin/News/harm_lists'));
			}else{

				$this->error('修改失败!','',1);
			}

		}

	}
	/**
	危害简介结束
	**/

	/**
		常见问题开始
	**/
	/**
	 * 常见问题列表
	 * 
	 */
	
	public function faq_lists(){

		if(!IS_POST){

			$User = M('news');

			/**
			**数据分页显示开始
			**/
			$count      = $User->where(array('active' => 1,'news_type' => 2))->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $User->where(array('active' => 1,'news_type' => 2))->order('rank desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			/**
			**数据分页结束
			**/
			$collections = M('collections');
			$comments = M('comments');
			foreach ($list as $k => $v) {
				# code...
				$list[$k]['comment_count'] = $comments->where(array('ctype' => 5,'for_id' => $v['id']))->count();
				$list[$k]['collection_count'] = $collections->where(array('ctype' => 5,'for_id' => $v['id']))->count();
			}

			$this->assign('show',$show);
			$this->assign('list',$list);
			$this->display();
		}
	}
	

	//添加常见问题
	public function add_faq(){

		if(IS_POST){

			$title = I('post.title');
			if(!empty($title)){

				$data['title'] = $title;
			}

			$content = I('post.content');
			if(!empty($content)){

				$data['content'] = $content;
			}

			$rank = I('post.rank');
			if(!empty($rank)){

				$data['rank'] = $rank;
			}

			if(!empty($_FILES['thumb']['name'])){

				$data['thumb'] =__ROOT__.'/Uploads'.'/'.$this->upload($_FILES['thumb']);
			}

			// print_r($data); die;
			$data['ctime'] = I('server.REQUEST_TIME');
			$data['news_type'] = 2;

			$User = M('news');

			$result = $User->data($data)->add();
			if(!empty($result)){

				$this->success('添加成功',U('Admin/News/faq_lists'));
			}else{

				$this->error('添加失败!');
			}

		}else{

			$this->display();
		}
	}


	//资讯详情显示
	public function faq_details(){

		if(IS_GET){

			$id = I('get.id');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$User = M('news');

			$list = $User->where($map)->find();

			$list['content'] = html_entity_decode($list['content']);

			$this->assign('list',$list);
			$this->display();
		}
	}

	//修改资讯
	public function modify_faq(){

		if(IS_GET){

			$id = I('get.id');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$User = M('news');

			$list = $User->where($map)->find();
			// $list['content'] = html_entity_decode($list['content']);
			$list['content'] = htmlspecialchars_decode($list['content']);
			$this->assign('list',$list);
			$this->display();
			
		}else{

			$id = I('post.id');
			if(!empty($id)){

				$map['id'] = $id;
			}else{

				$this->error('参数错误!','',1);
			}

			$title = I('post.title');
			if(!empty($title)){

				$data['title'] = $title;
			}


			$content = I('post.content');
			if(!empty($content)){

				$content = htmlspecialchars_decode($content);
				$data['content'] = trim(strip_tags($content));
			}
			// print_r($data); die;
			$rank = I('post.rank');
			if(!empty($rank)){

				$data['rank'] = $rank;
			}

			if(!empty($_FILES['thumb']['name'])){

				$data['thumb'] =__ROOT__.'/Uploads'.'/'.$this->upload($_FILES['thumb']);
			}

			// echo $data['stime']; die;
			$data['ctime'] = I('server.REQUEST_TIME');

			$User = M('news');

			$result = $User->where($map)->data($data)->save();

			if(!empty($result)){

				$this->success('添加成功!',U('Admin/News/faq_lists'));
			}else{

				$this->error('添加失败!','',1);
			}

		}

	}
	/**
	常见问题结束
	**/


	/**
		食事周刊开始
	**/
	/**
	 * 食事周刊列表
	 * 
	 */
	
	public function weekly_lists(){

		if(!IS_POST){

			$User = M('news');

			/**
			**数据分页显示开始
			**/
			$count      = $User->where(array('active' => 1,'news_type' => 3))->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $User->where(array('active' => 1,'news_type' => 3))->order('rank desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			/**
			**数据分页结束
			**/
			$collections = M('collections');
			$comments = M('comments');
			foreach ($list as $k => $v) {
				# code...
				$list[$k]['comment_count'] = $comments->where(array('ctype' => 6,'for_id' => $v['id']))->count();
				$list[$k]['collection_count'] = $collections->where(array('ctype' => 6,'for_id' => $v['id']))->count();
			}

			$this->assign('show',$show);
			$this->assign('list',$list);
			$this->display();
		}
	}
	

	//添加
	public function add_weekly(){

		if(IS_POST){

			$title = I('post.title');
			if(!empty($title)){

				$data['title'] = $title;
			}

			$content = I('post.content');
			if(!empty($content)){

				$data['content'] = $content;
			}

			$rank = I('post.rank');
			if(!empty($rank)){

				$data['rank'] = $rank;
			}

			if(!empty($_FILES['thumb']['name'])){

				$data['thumb'] =__ROOT__.'/Uploads'.'/'.$this->upload($_FILES['thumb']);
			}

			// print_r($data); die;
			$data['ctime'] = I('server.REQUEST_TIME');
                        $data['stime'] = I('server.REQUEST_TIME');
			$data['news_type'] = 3;

			$User = M('news');

			$result = $User->data($data)->add();

			//添加标签和文章关联
			$DB = M('food_tags_relationship');
			$tags = I('post.tags');
			foreach ($tags as $key => $value) {
				# code...
				$tags_data[$key]['aid'] = $result;
				$tags_data[$key]['tagid'] = $value;
				$tags_data[$key]['ctime'] = I('server.REQUEST_TIME');
			}
			// print_r($tags_data); die;
			$re = $DB->addAll($tags_data);

			if(!empty($re)){

				$this->success('添加成功',U('Admin/News/weekly_lists'));
			}else{

				$this->error('添加失败!');
			}
			// if(!empty($result)){

			// 	$this->success('添加成功',U('Admin/News/weekly_lists'));
			// }else{

			// 	$this->error('添加失败!');
			// }

		}else{

			$DB = M('news_tags');
			$tags = $DB->select();

			$this->assign('tags',$tags);
			$this->display();
		}
	}


	//详情显示
	public function weekly_details(){

		if(IS_GET){

			$id = I('get.id');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$User = M('news');

			$list = $User->where($map)->find();

			$list['content'] = html_entity_decode($list['content']);

			$this->assign('list',$list);
			$this->display();
		}
	}

	//修改资讯
	public function modify_weekly(){

		if(IS_GET){

			$id = I('get.id');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$User = M('news');

			$list = $User->where($map)->find();
			$list['content'] = html_entity_decode($list['content']);

			//获取标签
			$DB_tags = M('news_tags');
			$tags_id = $DB_tags->select();

			$DB_rel = M('food_tags_relationship');
			$tagid = $DB_rel->where(array('aid' => $id))->getField('tagid',true);

			foreach ($tags_id as $key => $value) {
				# code...
				if(in_array($value['id'], $tagid)){

					$tags_id[$key]['checkbox'] = 1;
				}else{
					$tags_id[$key]['checkbox'] = 0;
				}
			}

			// print_r($tag_id); die;
			$this->assign('tags_id',$tags_id);


			$this->assign('list',$list);
			$this->display();
		}else{

			$id = I('post.id');
			if(!empty($id)){

				$map['id'] = $id;
			}else{

				$this->error('参数错误!','',1);
			}

			$title = I('post.title');
			if(!empty($title)){

				$data['title'] = $title;
			}


			$content = I('post.content');
			if(!empty($content)){

				$data['content'] = $content;
			}

			$rank = I('post.rank');
			if(!empty($rank)){

				$data['rank'] = $rank;
			}

			if(!empty($_FILES['thumb']['name'])){

				$data['thumb'] =__ROOT__.'/Uploads'.'/'.$this->upload($_FILES['thumb']);
			}

			// echo $data['stime']; die;
			$data['ctime'] = I('server.REQUEST_TIME');

			$User = M('news');

			$result = $User->where($map)->data($data)->save();

			//修改标签关联
			$tags = I('post.tags');
			

			if(!empty($result)){

				//删除原有的标签关联
				M('food_tags_relationship')->where(array('aid' => $id))->delete();

				foreach ($tags as $key => $value) {
					# code...
					$rel_data[$key]['ctime'] = I('server.REQUEST_TIME');
					$rel_data[$key]['aid'] = $id;
					$rel_data[$key]['tagid'] = $value;
				}

				$modify_re = M('food_tags_relationship')->addAll($rel_data);

				if($modify_re){

					$this->success('修改成功!',U('Admin/News/weekly_lists'));
				}else{

					$this->error('修改失败!','',1);
				}
			}else{

				$this->error('修改失败!','',1);
			}

			// if(!empty($result)){

			// 	$this->success('添加成功!',U('Admin/News/weekly_lists'));
			// }else{

			// 	$this->error('添加失败!','',1);
			// }

		}

	}
	/**
	食事周刊结束
	**/


	/**
	 * 客服对评论进行回复
	 */
	public function set_comments(){

		if(IS_AJAX){

			$map['id'] = I('post.id');
			$data['pid'] = $map['id'];
			$data['content'] = trim(I('post.comments'));

			$DB = M('comments');

			$data['mid'] = 1;
			$data['ctime'] = I('server.REQUEST_TIME');
			$data['ctype'] = I('post.ctype');
			$data['for_id'] = I('post.for_id');
			$data['ishidden'] = 0;

			$re = $DB->data($data)->add();
			if(!empty($re)){

				$DB->where($map)->setInc('comment_count');
				echo json_encode(1);
			}
		}
	}



	/**
	 * ajax添加新的咨询标签
	 */
	public function ajax_add_tag(){

		if(IS_AJAX){

			$data['tag_name'] = trim(I('post.tag_name'));
			$data['ctime'] = I('server.REQUEST_TIME');

			$DB = M('news_tags');

			$re = $DB->data($data)->add();
			if(!empty($re)){

				echo json_encode($re);
			}else{

				echo json_encode(0);
			}
		}
	}


	/**
	 * ajax添加新的咨询标签
	 */
	public function ajax_add_food_tag(){

		if(IS_AJAX){

			$data['tag_name'] = trim(I('post.tag_name'));
			$data['ctime'] = I('server.REQUEST_TIME');

			$DB = M('food_tags');

			$re = $DB->data($data)->add();
			if(!empty($re)){

				echo json_encode($re);
			}else{

				echo json_encode(0);
			}
		}
	}

}